package edu.ycu.aladdin.web.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author <a href="mailto:wangruihuano@gmail.com">王瑞环</a>
 * @since 1.0
 */
@Data
public class RestResponse implements Serializable {

    private Integer code;

    private String msg;

    private Object data;

    public RestResponse(Type response, Object data) {
        this.code = response.code;
        this.msg = response.getMsg();
        this.data = data;
    }

    public static RestResponse successResponse(Object data){
        return new RestResponse(Type.SUCCESS,data);
    }


    public enum Type{
        /**
         * To define internal service status.
         */
        SUCCESS(0,"success"),
        PARAM_INVALID(420,"the parameter is invalid"),
        SERVER_ERROR(540,"server error"),
        SERVICE_EXCEPTION(541,"service exception"),
        OAUTH_FAIL(601,"oauth fail");

        private final int code;
        private final String msg;

        Type(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }
}
